import { createRouter, createWebHistory } from 'vue-router'

import Login from '@/views/login.vue'
import My from '@/views/my.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/', redirect: '/login'

    },
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/my',
      name: 'my',
      component: My,
    },

  ],
})
router.beforeEach((to, from, next) => {
  if ( to.fullPath == '/my') {
    let token = localStorage.getItem('token');
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
